package cn.iocoder.yudao.module.call.controller.admin.ivralert;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivralert.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivralert.IvrAlertDO;
import cn.iocoder.yudao.module.call.service.ivralert.IvrAlertService;

@Tag(name = "管理后台 - 敏感字")
@RestController
@RequestMapping("/call/ivr-alert")
@Validated
public class IvrAlertController {

    @Resource
    private IvrAlertService ivrAlertService;

    @PostMapping("/create")
    @Operation(summary = "创建敏感字")
    @PreAuthorize("@ss.hasPermission('call:ivr-alert:create')")
    public CommonResult<Long> createIvrAlert(@Valid @RequestBody IvrAlertSaveReqVO createReqVO) {
        return success(ivrAlertService.createIvrAlert(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新敏感字")
    @PreAuthorize("@ss.hasPermission('call:ivr-alert:update')")
    public CommonResult<Boolean> updateIvrAlert(@Valid @RequestBody IvrAlertSaveReqVO updateReqVO) {
        ivrAlertService.updateIvrAlert(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除敏感字")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-alert:delete')")
    public CommonResult<Boolean> deleteIvrAlert(@RequestParam("id") Long id) {
        ivrAlertService.deleteIvrAlert(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得敏感字")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-alert:query')")
    public CommonResult<IvrAlertRespVO> getIvrAlert(@RequestParam("id") Long id) {
        IvrAlertDO ivrAlert = ivrAlertService.getIvrAlert(id);
        return success(BeanUtils.toBean(ivrAlert, IvrAlertRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得敏感字分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-alert:query')")
    public CommonResult<PageResult<IvrAlertRespVO>> getIvrAlertPage(@Valid IvrAlertPageReqVO pageReqVO) {
        PageResult<IvrAlertDO> pageResult = ivrAlertService.getIvrAlertPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrAlertRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出敏感字 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-alert:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrAlertExcel(@Valid IvrAlertPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrAlertDO> list = ivrAlertService.getIvrAlertPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "敏感字.xls", "数据", IvrAlertRespVO.class,
                        BeanUtils.toBean(list, IvrAlertRespVO.class));
    }

}